package parser;

import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import bean.FileEntityBean;
import bean.FileHdfsBean;


public class JsonParser {
    public static FileHdfsBean ParseHdfs(String jsonStr){
        FileHdfsBean bean = null;
        try {
            JSONObject jsonObject = JSON.parseObject(jsonStr).getJSONObject("message").getJSONObject("request");
            JSONObject properties = jsonObject.getJSONObject("properties");
            String deviceId = jsonObject.getString("device_no");
            String deviceType = jsonObject.getString("device_type");
            String uid = deviceId == null ? jsonObject.getString("uid") : deviceId;
            String uid_type = deviceId == null ? jsonObject.getString("uid_type") : deviceType;
            String agent = jsonObject.getString("agent");
            String ip = jsonObject.getString("ip");
            String event = jsonObject.getString("event");
            String timestamp = jsonObject.getString("timestamp");
            String time = jsonObject.getString("time");
            int game_id = jsonObject.getIntValue("game_id");
            String timezone = jsonObject.getString("timezone");
            Map<String, String> map = JSONObject.parseObject(properties.toJSONString(), new TypeReference<Map<String, String>>() {
            });
            bean = new FileHdfsBean(event, uid, uid_type, agent, ip, timestamp, time, game_id, timezone);
            bean.setMap2Str(map);
        } catch (Exception e) {
            System.out.println("ParseHdfs Exception:" + e.getMessage());
        }
        return bean;
    }

    public static FileEntityBean ParseEntity(String jsonStr){
        FileEntityBean bean = null;
        try {
            JSONObject jsonObject = JSON.parseObject(jsonStr).getJSONObject("message").getJSONObject("request");
            JSONObject properties = jsonObject.getJSONObject("properties");
            String deviceId = jsonObject.getString("device_no");
            String deviceType = jsonObject.getString("device_type");
            String uid = deviceId == null ? jsonObject.getString("uid") : deviceId;
            String uid_type = deviceId == null ? jsonObject.getString("uid_type") : deviceType;
            String agent = jsonObject.getString("agent");
            String ip = jsonObject.getString("ip");
            String timestamp = jsonObject.getString("timestamp");
            String time = jsonObject.getString("time");
            int game_id = jsonObject.getIntValue("game_id");
            String timezone = jsonObject.getString("timezone");
            Map<String, String> map = JSONObject.parseObject(properties.toJSONString(), new TypeReference<Map<String, String>>() {
            });
            bean = new FileEntityBean(uid, uid_type, agent, ip, timestamp, time, game_id, timezone, map);
        } catch (Exception e) {
            System.out.println("FileEntityBean Exception:" + e.getMessage());
        }
        return bean;
    }
}
